var Login = {
	init: function () {
		this.initNode();
		this.showNotice();
		this.initAnimation();
        this.addEvent();
        this.t = 60;
        this.time = 0;
        this.inter = '';
        this.getTime();
        get('userInfo',7*24*60*60*1000);
	},
	initNode: function () {
		// 初始化节点
		this.$account = $('#j-account');
		this.$pwd = $('#j-secret');
		this.$errorMsg = $('#j-errorMsg');
		this.$loginBtn = $('#j-loginBtn');
		this.$footer = $('#footer');
		this.$getVali = $('#j-getVali');
	},

	initAnimation: function () {
		// 添加动画
		var $wrapper = $('#j-wrapper'),
			wrapperClass = $wrapper.attr('class');
		$wrapper
			.addClass('fadeInDown animated')
			.one(
				'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
				function () {
					$(this)
						.removeClass()
						.addClass(wrapperClass);
				}
			);
	},

	/**
	 * 如果浏览器非IE10,Chrome, FireFox, Safari, Opera的话，显示提示
	 */
	showNotice: function () {
		var browser = this.getBrowser(),
			temp = browser.split(' '),
			appname = temp[0],
			version = temp[1];
		if (['msie', 'firefox', 'opera', 'safari', 'chrome'].contains(appname)) {
			if (appname == 'msie' && version < 10) {
				this.$footer.find('p').removeClass('hide');
			}
		} else {
			this.$footer.find('p').removeClass('hide');
		}
	},
    getTime:function () {
        var that = this;
        this.time = sessionStorage.getItem('time');
        if(this.time > 0 && this.time){
            this.$getVali.html(this.time);
            this.t = this.time;
            this.inter = setInterval(that.timerInter.bind(this), 1000);
        }
    },
	addEvent: function () {
		// 绑定事件
		var that = this;
		this.$loginBtn.on('click', this.validate.bind(this));
		$(document).on('keydown', function (e) {
			var ev = e || window.event;
			if (ev.keyCode === 13) {
				that.validate();
			}
		});
		this.$getVali.on('click',this.getVali.bind(this));
	},
	getVali:function () {
		
        var that = this,
        pwd = that.$pwd.val(),
		errorMsg = '',
		
		account = $.trim(that.$account.val());
		if (account.length === 0) {
			errorMsg = '帐号不能为空';
			that.$errorMsg.html(errorMsg).removeClass('hide'); // 显示错误信息
		}else{
            if(that.t < 60){
                return "";
            }else{
                $.ajax({
                    url: SMSCodeInterface,
                    type: 'post',
                    data: 'json',
                    data: {
                        phone: account,
                        type:'login',
                    },
                    success: function (res) {
                        console.log(JSON.parse(res))
                        var data = JSON.parse(res);
                        if(data.code != '0'){
                            errorMsg = data.desc;
                            that.$errorMsg.html(errorMsg).removeClass('hide');
                        }else{
                            
                            //var time = document.getElementsByClassName("clock")[0];
                            //var time = that.$getVali.html(t);
                        
                           
                            that.inter = setInterval(that.timerInter.bind(that), 1000);
                        }
                    }
                })
            }
			
		}

    },
    timerInter:function () {
        var that = this;
        this.t--;
        sessionStorage.setItem("time",this.t);
        this.$getVali.html(this.t);
        if(that.t <= 0) {
            // location.href = "https://www.baidu.com";
            this.$getVali.html('获取验证码');
            this.t = 0;
            sessionStorage.setItem("time",this.t);
            clearInterval(this.inter);
        }
    },
	validate: function () {
		// 登录验证
		var that = this,
			account = $.trim(that.$account.val()),
			pwd = that.$pwd.val(),
			errorMsg = '';
		if (account.length === 0) {
			errorMsg = '帐号不能为空';
		} else if (!pwd || pwd.length < 6) {
			errorMsg = '验证码至少6位';
		} else {
			that.$loginBtn.html('登录中...').attr('disabled', 'disabled');
			that.requestLogin.call(that, account, pwd);
			that.$loginBtn.html('登录').removeAttr('disabled');
		}
		that.$errorMsg.html(errorMsg).removeClass('hide'); // 显示错误信息
		return false;
	},
	//这里做了个伪登录方式（实际上是把accid，token带到下个页面连SDK在做鉴权）
	//一般应用服务器的应用会有自己的登录接口
	requestLogin: function (account, pwd) {
		$.ajax({
			url: loginInterface,
			type: 'post',
			data: 'json',
			data: {
				phone: account,
				code: pwd,
			},
			success: function (res) {
				console.log(JSON.parse(res))
                var userInfo = JSON.parse(res).data;
                set('userInfo',userInfo)
				//localStorage.setItem('userInfo',JSON.stringify(userInfo));
				setCookie('uid', userInfo.name.toLocaleLowerCase());
				setCookie('sdktoken', userInfo.token);
				window.CONFIG.appkey = userInfo.key;
				if (/chatroom/.test(location.href)) {
                    delCookie('nickName');
                    
					window.location.href = './list.html';
				} else {
					window.location.href = './main.html';
				}
			}
		})
		/*  setCookie('uid', account.toLocaleLowerCase());
		 //自己的appkey就不用加密了
		 // setCookie('sdktoken',pwd);
		 if (
		   window.CONFIG.appkey === 'fe416640c8e8a72734219e1847ad2547' ||
		   window.CONFIG.appkey === '45c6af3c98409b18a84451215d0bdd6e'
		 ) {
		   var password = MD5(pwd);
		 } else {
		   password = pwd;
		 }
		 setCookie('sdktoken', password);
		 if (/chatroom/.test(location.href)) {
			 delCookie('nickName');
			 window.location.href = './list.html';
		 } else {
			 window.location.href = './main.html';
		 } */
	},

	/**
	 * 获取浏览器的名称和版本号信息
	 */
	getBrowser: function () {
		var browser = {
			msie: false,
			firefox: false,
			opera: false,
			safari: false,
			chrome: false,
			netscape: false,
			appname: 'unknown',
			version: 0
		},
			ua = window.navigator.userAgent.toLowerCase();
		if (/(msie|firefox|opera|chrome|netscape)\D+(\d[\d.]*)/.test(ua)) {
			browser[RegExp.$1] = true;
			browser.appname = RegExp.$1;
			browser.version = RegExp.$2;
		} else if (/version\D+(\d[\d.]*).*safari/.test(ua)) {
			// safari
			browser.safari = true;
			browser.appname = 'safari';
			browser.version = RegExp.$2;
		}
		return browser.appname + ' ' + browser.version;
	}
};
Login.init();
